package com.njym.java.security.admin.rest;

import com.github.pagehelper.PageHelper;
import com.njym.java.security.admin.biz.FamilyUserBiz;
import com.njym.java.security.admin.biz.TaskClassifyBiz;
import com.njym.java.security.admin.biz.TaskListBiz;
import com.njym.java.security.admin.biz.TaskUserRelevanceBiz;
import com.njym.java.security.admin.entity.*;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("taskUserRelevance")
public class TaskUserRelevanceController extends BaseController<TaskUserRelevanceBiz, TaskUserRelevance> {
    @Autowired
    private TaskClassifyBiz taskClassifyBiz;
    @Autowired
    private TaskListBiz taskListBiz;
    @Autowired
    private FamilyUserBiz familyUserBiz;
    @Autowired
    private TaskUserRelevanceBiz taskUserRelevanceBiz;

    /**
     * 指导师任务评星页面获取初始数据
     * @param page
     * @param limit
     * @param instructorId
     * @return
     */
    @RequestMapping(value = "/getTasksStars",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<TaskUserRelevanceVo> getTasksStars(@RequestParam(defaultValue = "1") int page,
                                                                 @RequestParam(defaultValue = "20") int limit,
                                                                 Integer instructorId, Integer modelFlag,
                                                                 String attr3, Integer status){
        PageHelper.startPage(page,limit);
        List<TaskUserRelevanceVo> list = taskUserRelevanceBiz.getTasksStars(instructorId, modelFlag, attr3, status);
        int total = taskUserRelevanceBiz.getcounts(instructorId, modelFlag, attr3, status);
        Map<String, Object> data = new HashMap<>(2);
        data.put("list", list);
        data.put("total", total);
        return new ObjectRestResponse<TaskUserRelevanceVo>().rel(true).data(data);
    }


    /**
     * 任务分配中，点击用户获得用户已经有的任务和可以分配的任务
     * @param familyId
     * @param conId
     * @param classId
     * @return
     */
    @RequestMapping(value = "/getUserTask",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<TaskUserRelevanceVo> getUserTask(@RequestParam int familyId, @RequestParam int conId, @RequestParam int classId){
        List<TaskUserRelevanceVo> list = baseBiz.getUserTask(familyId, conId);  //找到用户已有的任务
        List<Integer> ids = new ArrayList<>();  //list中未接受和未完成的任务的id
        for(TaskUserRelevanceVo item : list){
            if(item.getStatus() < 2){
                ids.add(item.getTaskId());
            }
        }
        List<TaskList> tasks = taskListBiz.getTasks(ids, classId);  //找到没有安排的任务
        Map<String, Object> data = new HashMap<>(2);
        data.put("list", list);
        data.put("tasks", tasks);
        return new ObjectRestResponse<>().rel(true).data(data);
    }

    /**
     * 通过任务id取消任务
     * @param id
     * @return
     */
    @RequestMapping(value = "/cancelTask",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<?> getUserTask(@RequestParam int id){
        TaskUserRelevance entity = new TaskUserRelevance();
        entity.setId(id);
        entity.setDeleted(1);
        baseBiz.updateSelectiveById(entity);
        return new ObjectRestResponse<>().rel(true);
    }

    /**
     * 任务分配页面获取页面初始数据
     * @param conId
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<?> lists(@RequestParam int conId){
//        任务分类
        List<TaskClassify> classify = taskClassifyBiz.getAllClassfy();
//        任务列表
        TaskList taskList = new TaskList();
        taskList.setDeleted(0);
        taskList.setTaskClassifyId(1);//默认查找分类1的
        List<TaskList> taskLists = taskListBiz.selectList(taskList);
//        咨询师手里的所有用户
        FamilyUser entity = new FamilyUser();
        entity.setTeacherId(conId);
        List<FamilyUser> familyUsers = familyUserBiz.selectList(entity);
        Map<String, Object> data = new HashMap<>(3);
        data.put("classify", classify);
        data.put("taskLists", taskLists);
        data.put("taskUsers", familyUsers);
        return new ObjectRestResponse<Object>().rel(true).data(data);
    }

    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    @ResponseBody
    public Object updateStatus(@RequestBody TaskUserRelevance taskUserRelevance){
        int status = 3;
        taskUserRelevance.setStatus(status);
        baseBiz.updateById(taskUserRelevance);
        return null;
    }



}